/**
 * 
 */
package es.ucm.fdi.xtremeaudio.vista;

import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * <ul>
 * <li><b>Proyecto:</b> XtremeAudio</li>
 * <li><b>Grupo:</b> 7 <i>(4&deg;B)</i></li>
 * <li><b>Asignatura:</b> Ingenier&iacute;a del Software</li>
 * <li><b>Curso:</b> 2010/2011</li>
 * <li><b>Centro:</b> Universidad Complutense de Madrid</li>
 * </ul>
 * 
 * El <b>MoverPanelListener</b> implementa {@link MouseListener} y
 * {@link MouseMotionListener} para permitir mover las distintas ventanas de la
 * aplicaci&;oacuten mediante &quot;arrastar y soltar&quot;.
 * 
 * @author &Aacute;lvaro G&oacute;mez Porris
 * @author Elena Hern&aacute;ndez Delgado
 * @author Mar&iacute;a Trinidad Mart&iacute;n Campos
 * @author Mois&eacute;s Mart&iacute;nez Montes
 * @author Mat&iacute;as Salinero Delgado
 * 
 * @version %I%, %G%
 * 
 * @since 1.0
 */
public class MoverPanelListener implements MouseListener, MouseMotionListener {

	/**
	 * @param componente
	 * @return El {@link JFrame}.
	 */
	public static JFrame getFrame(Container componente) {
		if (componente instanceof JFrame) {
			return (JFrame) componente;
		}
		return getFrame(componente.getParent());
	}

	Point posInicial;
	Point locInicial;

	JComponent componente;

	/**
	 * @param target
	 */
	public MoverPanelListener(JComponent target) {
		this.componente = target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent
	 * )
	 */
	@Override
	public void mouseDragged(MouseEvent e) {
		Point current = getScreenLocation(e);
		Point offset = new Point(
				(int) current.getX() - (int) posInicial.getX(), (int) current
						.getY()
						- (int) posInicial.getY());
		JFrame frame = getFrame(componente);
		Point new_location = new Point(
				(int) (locInicial.getX() + offset.getX()), (int) (locInicial
						.getY() + offset.getY()));
		frame.setLocation(new_location);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseMoved(MouseEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e) {
		posInicial = getScreenLocation(e);
		locInicial = getFrame(componente).getLocation();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e) {
	}

	Point getScreenLocation(MouseEvent e) {
		Point cursor = e.getPoint();
		Point locComponente = componente.getLocationOnScreen();
		return new Point((int) (locComponente.getX() + cursor.getX()),
				(int) (locComponente.getY() + cursor.getY()));
	}

}